package com.siyue.controller;

import com.siyue.service.UserService;
import com.siyue.util.RedisUtil;
import com.siyue.util.RestResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private RedisUtil redisUtil;

    /**
     * 功能描述:获取验证码
     *
     * @param: [request]
     * @return: java.lang.String
     * @auther: wuhao
     * @date: 2020/8/6 14:15
     */
    @RequestMapping(value = "getCode", method = RequestMethod.POST)
    public RestResponse getCode(HttpServletRequest request) {
        RestResponse result = new RestResponse();
        userService.getCode(result, request);
        return result;
    }

    /**
     * 功能描述:密码登录
     *
     * @param: [request, username, password, code]
     * @return: java.lang.String
     * @auther: wuhao
     * @date: 2020/8/6 14:15
     */
    @RequestMapping(value = "userLogin", method = RequestMethod.POST)
    public RestResponse userLogin(HttpServletRequest request, String username, String password, String code) {
        return userService.userLogin(username, password, code, request);
    }

    /**
     * 功能描述:退出登录
     *
     * @param: [access_token]
     * @return: java.lang.String
     * @auther: wuhao
     * @date: 2020/8/6 17:36
     */
    @RequestMapping(value = "userLogout", method = RequestMethod.POST)
    public RestResponse userLogout(String access_token) {
        RestResponse result = new RestResponse();
        try {
            redisUtil.del("PREFIX_USER_TOKEN_" + access_token);
            result.setStatus("success");
            result.setMessage("退出成功");
        } catch (Exception e) {
            result.setStatus("success");
            result.setMessage("退出失败");
        }
        return result;

    }
}

